import { createSlice } from '@reduxjs/toolkit'
import axios from "axios";

const channelStore = createSlice({
  // 模块名称独一无二
  name: 'channel',
  // 初始数据
  initialState: {
    channel: []
  },
  // 修改数据的同步方法
  reducers: {
    setChannel(state, action) {
      state.channel = action.payload;
    }
  }
})


const { setChannel } = channelStore.actions;

const url = 'http://geek.itheima.net/v1_0/channels';
const fetchChannelList = () => {
  return async (dispatch) => {
    const res = await axios.get(url)
    dispatch(setChannel(res.data.data.channels))
  }
}

export { fetchChannelList }

const channelReducer = channelStore.reducer;
export default channelReducer;